package com.yc.config;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Snowflake算法配置类
 * 1. 从Nacos获取workerId和datacenterId配置
 * 2. 创建Snowflake实例并注入Spring容器
 */
@Configuration
public class SnowflakeConfig {

    // 从Nacos配置中心获取workerId，默认值为1，配置变更自动刷新
    @NacosValue(value = "${snowflake.worker-id:1}", autoRefreshed = true)
    private long workerId;

    // 从Nacos配置中心获取datacenterId，默认值为1，配置变更自动刷新
    @NacosValue(value = "${snowflake.datacenter-id:1}", autoRefreshed = true)
    private long datacenterId;

    /**
     * 创建Snowflake实例
     * @return Snowflake ID生成器
     */
    @Bean
    public Snowflake snowflake() {
        // 使用Hutool工具类创建Snowflake实例
        return IdUtil.getSnowflake(workerId, datacenterId);
    }
}
